package com.apes.framework.plugin.esb.processor.jpa;

import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.jpa.spec.model.Group;
import com.apes.framework.plugin.esb.api.Request;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.plugin.esb.processor.AbstractChainProcessor;
import com.apes.framework.util.EventUtil;
import com.apes.framework.util.MapUtil;
import com.apes.framework.util.Operation;
import lombok.Data;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 功能：JPA查询权限
 *
 * @author xul
 * @create 2018-07-11 9:56
 */
@Data
public class JpaAuthProcessor<T extends Request, R> extends AbstractChainProcessor<T, R> {
    private String field;  //字段
    private String operator = "in"; //操作
    private String purpose;  //所属、操作、查询、......
    private String type;  //公司、部门、仓库、网点、物流站点......

    @Override
    protected R doProcess(T source) {
        String postId = ((SimpleRequest) source).getPostId();
        if (StringUtils.isEmpty(postId)) return null;
        Map result = source.getMap();
        if (result.containsKey("condition")) {
            List<Map<String, Object>> filters = (List<Map<String, Object>>) result.get("condition");
            Map filter = getFilter(postId);
            if (filter != null) filters.add(filter);
        } else if (result.containsKey("group")) {
            Map filter = getFilter(postId);
            if (filter != null) {
                Map group = new HashMap();
                group.put("operator", "and");
                List groups = new ArrayList();
                groups.add(result.get("group"));
                group.put("groups", groups);
                List<Map> filters = new ArrayList<>();
                filters.add(filter);
                group.put("filters", filters);
                result.put("group", group);
            }
        } else {
            List<Map> filters = JpaDsl.getFilters(source.getMap());
            Map filter = getFilter(postId);
            if (filter != null) filters.add(filter);
            result.put("condition", filters);
        }
        return (R) result;
    }

    public Map getFilter(String postId) {
        List<String> values = getOrgIds(postId, purpose, type);
        if (values == null) return null;
        Map filter = new HashMap();
        filter.put("operator", Operation.getSearchType(operator.trim()));
        filter.put("field", field);
        filter.put("value", values);
        return filter;
    }

    private List<String> getOrgIds(String postId, String purpose, String type) {
        String topic = "event:aut.post.findLocalPermitItems";
        Map body = MapUtil.mapper("postId", postId, "purpose", purpose, "type", type);
        List<String> orgIds = (List<String>) EventUtil.sendSynEvent(this, topic, body).stream().findFirst().get();
        if (orgIds == null) orgIds = new ArrayList<>();
        if (operator.equals("like")) {
            orgIds = orgIds.stream().map(orgId -> orgId + "%").collect(Collectors.toList());
        }
        if (orgIds.isEmpty()) return null; // orgIds.add("1 != 1");
        return orgIds;
    }
}
